package net.youball.action;

import java.util.Date;


import net.youball.conf.entity.Captcha;
import net.youball.entity.BallLoginInfo;
import net.youball.entity.BallUser;
import net.youball.service.IUserService;
import net.youball.utils.IpUtil;
import net.youball.utils.String.MD5;
import net.youball.utils.String.RandomUtils;
import net.youball.utils.String.StringUtils;
import net.youball.utils.mail.MailInfo;
import net.youball.utils.mail.MailSend;

import org.codehaus.jackson.map.ObjectMapper;

import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends BaseAction implements ModelDriven<BallUser>{

/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IUserService userService;
	
	private BallUser user=new BallUser();
	
	public String validArgs(){
		String str=null;
		try{
		//ObjectMapper mapper=new ObjectMapper();
		
		//Map<String,Object> arg=mapper.readValue(args, Map.class);
		String name=request.getParameter("name");
		String value=request.getParameter("value");
		
		
		if(!StringUtils.compareTrim(name,value))return null;
			boolean bool=userService.doValid(name.trim(), value.trim());
			System.out.println(name);
			System.out.println(bool);
			if(bool)
				str="{\"state\":true}";
			else
				str="{\"state\":false}";
			this.outJson(str);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
		
	}
	public String getSession() {
	
		
		System.out.println("获取session id："+session.toString());
		this.outJson("session:"+session.toString());
		return null;
	}
	public String login() {
		// TODO Auto-generated method stub
		
		
		try{
			ObjectMapper mapper=new ObjectMapper();
			///Map<String,Object> arg=mapper.readValue(args, Map.class);
			
			String name=request.getParameter("email");
			String password=request.getParameter("password");
			
			System.out.println(name+"::"+password+"::");
			if(!StringUtils.compareTrim(name,password))return null;

			BallUser user= userService.doEmailLogin(name.trim(), MD5.parse(password.trim()));
			
			//HttpServletRequest request= ServletActionContext.getRequest();
			
			if(user!=null){
				user.setPassWord("");			
				session.put("user", user);
				this.outJson(mapper.writeValueAsString(user));
			}
		}catch(Exception e){
			this.outJson("{\"state\":false}");
			e.printStackTrace();
		}
	
		return null;
	}
	public void logout() {
		
		session.clear();
		
	}

	public String validReg(){
		String id=request.getParameter("id");
		String code=request.getParameter("code");
		String time=request.getParameter("time");
		Captcha cap=(Captcha) context.getAttribute(id);
		if(cap!=null){
			if(time!=null){
				Long s=Long.parseLong(time);
				Long j=s-cap.getTime();
				if(j>1000*60*60*5){
					//超时了;
					this.outJson("{\"state\":\"timeout\"}") ;
					return null;
				}
			}
			
			if(code!=null&&code.equals(cap.getCode())){
				//验证成功
				this.outJson("{\"state\":true}");
				return null;
			}else{
				this.outJson("{\"state\":false}");
				return null;
			}
		}else{
			this.outJson("{\"state\":\"error\"}");
		}
		return null;
	}
	
	public String reg() {
		// TODO Auto-generated method stub
		
		 
		try{
			ObjectMapper mapper=new ObjectMapper();
			user.setPassWord(MD5.parse(user.getPassWord()));
			user.setState(0);
			BallLoginInfo loginInfo=new BallLoginInfo();
			//loginInfo.setLoginIp(arg.get("ip").toString());
			//loginInfo.setLoginLocation(arg.get("localtion").toString());
			String str=request.getParameter("systeminfo");
			loginInfo.setLoginSystem(str);
			loginInfo.setLoginIp(IpUtil.getIp(request));
			//loginInfo.setLoginSystem("system:"+arg.get("system")+",version:"+arg.get("version")+",uuid:"+arg.get("uuid"));
			loginInfo.setLoginTime(new Date().getTime());
			
			Integer id=userService.doReg(this.user, loginInfo);
			Captcha cap=new Captcha(8);
			
			context.setAttribute(id.toString(),cap);
			this.outJson(mapper.writeValueAsString(user));
			
			StringBuffer context=new StringBuffer("亲爱的用户，您得用户名: "+this.user.getUserName()+"  衷心感谢您注册了账号，为了方便像您发送提醒信息，保证您的账号安全，请您绑定邮箱。我们保证 绝对不会泄露您的邮箱或发送垃圾邮件!</br>") ;
			context.append("<a href=\"http://127.0.0.1:8080/YouBallService/userAction!validReg?id="+id+"&code="+cap.getCode()+"&time="+cap.getTime()+"\">立即验证</a></br>");
			context.append("如果不能点击 请复制链接到地址栏输入，5小时内有效！！");
			context.append("http://127.0.0.1:8080/YouBallService/userAction!validReg?id="+id+"&code="+cap.getCode()+"&time="+cap.getTime());
			MailSend.sendOutEmail("user", "YouBall邮箱验证", context.toString(), this.user.getUserEmail());
			
			//userService.add(user);
		}catch(Exception e){
			this.outJson("{\"state\":false}");
			e.printStackTrace();
		}
		return null;
		
		
	}
	

	public String message(String name,String value) {
		// TODO Auto-generated method stub
		/*MessageContext mess=new MessageContext();
		Session s= mess.getSession();*/
	
		return name;
	}
	public String messages() {
		// TODO Auto-generated method stub
		/*MessageContext mess=new MessageContext();
		Session s= mess.getSession();*/
		
		//MessageContext mess=AbstractInvoker.getContext();
		//Session s= mess.getSession();
		//BallUser user= userService.findById(Integer.parseInt(name));
		userService.Test();
		return "";
	}


	
	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	public BallUser getModel() {
		// TODO Auto-generated method stub
		return user;
	}
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		 
		return null;
	}
	public BallUser getUser() {
		return user;
	}
	public void setUser(BallUser user) {
		this.user = user;
	}

	
	
}
